﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import Engine.Classes.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Classes.*;
    import Modules.storage.*;
    import Modules.crew.*;

    public class GatedStorageMechanic extends BaseStorageUnit implements IActionGameMechanic, IStorage {

        public static const MODE_STORE:String = "store";
        public static const MODE_PLACE_FROM_STORAGE:String = "place_from_storage";

        protected var m_owner:MechanicMapResource;
        protected var m_config:MechanicConfigData;

        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        public function hasOverrideForGameAction(_arg1:String):Boolean{
            return (true);
        }
        protected function canExecute():Boolean{
            return (true);
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            if (!this.canExecute()){
                return (new MechanicActionResult(false, true, false, null));
            };
            var _local3:String = _arg2["mode"];
            if (_local3 == MODE_STORE){
                return (this.handleStore(_arg2));
            };
            if (_local3 == MODE_PLACE_FROM_STORAGE){
                return (this.handlePlaceFromStorage(_arg2));
            };
            return (new MechanicActionResult(false, true, false, null));
        }
        protected function handleStore(_arg1:Array):MechanicActionResult{
            var _local6:Boolean;
            var _local7:Boolean;
            var _local8:Boolean;
            var _local9:Object;
            var _local2:Array = this.m_owner.getDataForMechanic(this.m_config.type);
            var _local3:MapResource = _arg1["object"];
            var _local4:Boolean = this.store(_local3);
            var _local5:MechanicActionResult = new MechanicActionResult(false, true, false, null);
            if (_local4){
                _local6 = true;
                _local7 = false;
                _local8 = true;
                _local9 = {
                    operation:"storeObject",
                    objectId:_local3.getId()
                };
                _local5 = new MechanicActionResult(_local6, _local7, _local8, _local9);
            };
            return (_local5);
        }
        protected function handlePlaceFromStorage(_arg1:Array):MechanicActionResult{
            var _local2:Array = this.m_owner.getDataForMechanic(this.m_config.type);
            var _local3:int = _arg1["slotId"];
            var _local4:MapResource = _arg1["mapResource"];
            if (_local3 >= _local2.length){
                return (new MechanicActionResult(false, true, false, null));
            };
            this.handleNpcFlush();
            var _local5:int = _local4.getPosition().x;
            var _local6:int = _local4.getPosition().y;
            var _local7:int = _local4.getDirection();
            this.m_owner.npcPopularity = (this.m_owner.npcPopularity - _local4.npcPopularity);
            _local2.splice(_local3, 1);
            var _local8:Boolean;
            var _local9:Boolean;
            var _local10:Boolean;
            var _local11:Object = {
                operation:"removeObject",
                slotId:_local3,
                newX:_local5,
                newY:_local6,
                dir:_local7
            };
            return (new MechanicActionResult(_local8, _local9, _local10, _local11));
        }
        public function get numOpenSlots():int{
            var _local3:WorldObject;
            var _local1:int;
            var _local2:Array = this.m_owner.getDataForMechanic("slots");
            for each (_local3 in _local2) {
                if (_local3){
                    _local1++;
                };
            };
            return ((this.getStorageCapacity() - _local1));
        }
        public function getStorageCapacity():int{
            var _local1:int = this.m_owner.getItem().storageInitCapacity;
            var _local2:Crew = Global.crews.getCrewById(this.m_owner.getId());
            var _local3:int = (_local2) ? _local2.count : 0;
            return ((_local1 + _local3));
        }
        public function canStore(_arg1:MapResource):Boolean{
            var _local2:Boolean;
            if (StorageType.validateStorageItemType(_arg1, this.m_owner.getItemName())){
                _local2 = _arg1.isStorable();
            };
            return (_local2);
        }
        public function store(_arg1):Boolean{
            var _local3:Array;
            var _local2:Boolean;
            if ((_arg1 is MapResource)){
                _arg1.detach();
                _arg1.prepareForStorage();
                _arg1.onSell();
                _local3 = this.m_owner.getDataForMechanic(this.m_config.type);
                this.m_owner.npcPopularity = (this.m_owner.npcPopularity + (_arg1 as MapResource).npcPopularity);
                _local3.push(_arg1);
                this.m_owner.setDataForMechanic(this.m_config.type, _local3);
                _local2 = true;
            };
            return (_local2);
        }
        private function handleNpcFlush():void{
            MerchantCustomerMechanic.forceFlush();
        }

    }
}//package Mechanics.GameEventMechanics 
